/*   time:2019-06-13 20:39:23*/
var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(t) {
        return typeof t
    } : function(t) {
        return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : typeof t
    }, t = "function" == typeof Symbol && "symbol" == _typeof(Symbol.iterator) ? function(t) {
        return void 0 === t ? "undefined" : _typeof(t)
    } : function(t) {
        return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : void 0 === t ? "undefined" : _typeof(t)
    }, e = require("jquery");
module.exports = {
    toQueryPair: function(t, e) {
        return void 0 === e ? t : t + "=" + encodeURIComponent(null === e ? "" : String(e))
    },
    getUrl: function(o, n, i) {
        o = o.replace(/\//gi, ".");
        var a = getApp().getConfig().api + "&r=" + o;
        return n && ("object" == (void 0 === n ? "undefined" : t(n)) ? a += "&" + e.param(n) : "string" == typeof n && (a += "&" + n)), a
    },
    json: function(t, o, n, i, a, r) {
        var s = getApp(),
            c = s.getCache("userinfo_openid"),
            u = s.getCache("usermid"),
            f = s.getCache("authkey");
        (o = o || {}).comefrom = "wxapp", o.openid = "sns_wa_" + c, u && (o.mid = u.mid, o.merchid = o.merchid || u.merchid);
        var d = this;
        i && d.loading(), o && (o.authkey = f || "");
        var l = {
            url: (a ? this.getUrl(t) : this.getUrl(t, o)) + "&timestamp=" + +new Date,
            method: a ? "POST" : "GET",
            header: {
                "Content-type": a ? "application/x-www-form-urlencoded" : "application/json",
                Cookie: "PHPSESSID=" + c
            }
        };
        r || delete l.header.Cookie, a && (l.data = e.param(o)), n && (l.success = function(t) {
            if (i && d.hideLoading(), "request:ok" == t.errMsg && "function" == typeof n) {
                if (s.setCache("authkey", t.data.authkey || ""), void 0 !== t.data.sysset) {
                    if (1 == t.data.sysset.isclose) return void wx.redirectTo({
                        url: "/pages/message/auth/index?close=1&text=" + t.data.sysset.closetext
                    });
                    s.setCache("sysset", t.data.sysset)
                }
                n(t.data)
            }
        }), l.fail = function(t) {
            i && d.hideLoading(), d.alert(t.errMsg)
        }, wx.request(l)
    },
    post: function(t, e, o, n, i) {
        this.json(t, e, o, n, !0, i)
    },
    get: function(t, e, o, n, i) {
        this.json(t, e, o, n, !1, i)
    },
    getDistanceByLnglat: function(t, e, o, n) {
        function i(t) {
            return t * Math.PI / 180
        }
        var a = i(e),
            r = i(n),
            s = a - r,
            c = i(t) - i(o),
            u = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(s / 2), 2) + Math.cos(a) * Math.cos(r) * Math.pow(Math.sin(c / 2), 2)));
        return u *= 6378137, Math.round(1e4 * u) / 1e7
    },
    alert: function(e, o) {
        "object" === (void 0 === e ? "undefined" : t(e)) && (e = JSON.stringify(e)), wx.showModal({
            title: "提示",
            content: e,
            showCancel: !1,
            success: function(t) {
                t.confirm && "function" == typeof confirm && o()
            }
        })
    },
    confirm: function(e, o, n) {
        "object" === (void 0 === e ? "undefined" : t(e)) && (e = JSON.stringify(e)), wx.showModal({
            title: "提示",
            content: e,
            showCancel: !0,
            success: function(t) {
                t.confirm ? "function" == typeof o && o() : "function" == typeof n && n()
            }
        })
    },
    loading: function(t) {
        void 0 !== t && "" != t || (t = "加载中"), wx.showToast({
            title: t,
            icon: "loading",
            duration: 5e6
        })
    },
    hideLoading: function() {
        wx.hideToast()
    },
    toast: function(t, e) {
        e || (e = "success"), wx.showToast({
            title: t,
            icon: e,
            duration: 1e3
        })
    },
    success: function(t) {
        wx.showToast({
            title: t,
            icon: "success",
            duration: 1e3
        })
    },
    upload: function(n) {
        var i = this;
        wx.chooseImage({
            success: function(t) {
                i.loading("正在上传...");
                var e = i.getUrl("util/uploader/upload", {
                    file: "file"
                }),
                    o = t.tempFilePaths;
                wx.uploadFile({
                    url: e,
                    filePath: o[0],
                    name: "file",
                    success: function(t) {
                        i.hideLoading();
                        var e = JSON.parse(t.data);
                        if (0 != e.error) i.alert("上传失败");
                        else if ("function" == typeof n) {
                            var o = e.files[0];
                            n(o)
                        }
                    }
                })
            }
        })
    },
    pdata: function(t) {
        return t.currentTarget.dataset
    },
    data: function(t) {
        return t.target.dataset
    },
    phone: function(t) {
        var e = this.pdata(t).phone;
        wx.makePhoneCall({
            phoneNumber: e
        })
    },
    pay: function(e, o, n) {
        return "object" == (void 0 === e ? "undefined" : t(e)) && "function" == typeof o && (e.success = o, "function" == typeof n && (e.fail = n), void wx.requestPayment(e))
    },
    cartcount: function(e) {
        this.get("member/cart/count", {}, function(t) {
            e.setData({
                cartcount: t.cartcount
            })
        })
    },
    onShareAppMessage: function(t, e) {
        var o = getApp(),
            n = o.getCache("sysset"),
            i = n.share || {}, a = o.getCache("userinfo_id"),
            r = n.shopname || "",
            s = n.description || "";
        return i.title && (r = i.title), e && (r = e), i.desc && (s = i.desc), {
            title: r,
            desc: s,
            path: (t = -1 != (t = t || "/pages/index/index").indexOf("?") ? t + "&" : t + "?") + "mid=" + a
        }
    },
    str2Obj: function(t) {
        if ("string" != typeof t) return t;
        if (t.indexOf("&") < 0 && t.indexOf("=") < 0) return {};
        var o = t.split("&"),
            n = {};
        return e.each(o, function(t, e) {
            if (-1 < e.indexOf("=")) {
                var o = e.split("=");
                n[o[0]] = o[1]
            }
        }), n
    },
    countDown: function(t, e) {
        var o = parseInt(Date.now() / 1e3),
            n = 0;
        if (t && (n = o < t ? t - o : o - t, n = parseInt(n)), e && (n = parseInt(e)), 0 == n) return !1;
        var i = Math.floor(n / 86400),
            a = Math.floor((n - 24 * i * 60 * 60) / 3600),
            r = Math.floor((n - 24 * i * 60 * 60 - 3600 * a) / 60),
            s = Math.floor(n - 24 * i * 60 * 60 - 3600 * a - 60 * r);
        return [i, a < 10 ? "0" + a : a, r < 10 ? "0" + r : r, s < 10 ? "0" + s : s]
    }
};